Component({
    properties: {
        prize: {
            type: Array,
            value: []
        },
        start: {
            type: Boolean,
            value: !1
        },
        lottery: {
            type: Number,
            value: 0
        },
        minTurns: {
            type: Number,
            value: 3
        },
        maxTurns: {
            type: Number,
            value: 5
        },
        minDuration: {
            type: Number,
            value: 7
        },
        maxDuration: {
            type: Number,
            value: -1
        },
        debug: {
            type: Boolean,
            value: !1
        },
        angle: {
            type: Number,
            value: -1
        },
        infinite: {
            type: Boolean,
            value: !1
        }
    },
    ready: function() {
        !1 === this.data.debug && this.drawCircle();
        for (var t = this.data.prize, e = 0; e < t.length; e++) {
            t[e].angle = 360 / t.length * e + 180 / t.length;
        }
        this.fillLatticeColor(), t = t.reverse(), this.setData({
            itemAngle: 360 / this.data.prize.length,
            prize: t
        });
    },
    data: {
        lotteryRotateAngle: 0,
        transitionDuration: 0,
        transitionFunction: "linear",
        spinning: !1,
        itemAngle: 0,
        latticeColors: []
    },
    methods: {
        drawCircle: function() {
            var t = this;
            this.createSelectorQuery().select("#lotteryCanvas").fields({
                node: !0,
                size: !0
            }).exec(function(e) {
                var a = t.data.latticeColors;
                0 == a.length && (a = t.generateColors([ "#fff", "#feb446" ], t.data.prize.length));
                var n = a.length, i = e[0].node, r = wx.getSystemInfoSync().pixelRatio, o = i.getContext("2d"), l = e[0].width * r, s = e[0].height * r;
                i.width = l, i.height = s, o.translate(0, s), o.rotate(-90 * Math.PI / 180);
                var h = l / 2, u = 2 * Math.PI / n;
                o.clearRect(0, 0, l, s), o.strokeStyle = "transparent";
                for (var g = 0; g < n; g++) {
                    var d = g * u;
                    o.fillStyle = a[g], o.beginPath(), o.arc(.5 * l, .5 * s, h, d, d + u, !1), o.arc(.5 * l, .5 * s, 0, d + u, d, !0), 
                    o.stroke(), o.fill(), o.save();
                }
            });
        },
        startSpinning: function() {
            var t = this;
            if (!this.data.spinning) {
                var e = this.data.lottery * this.data.itemAngle, a = e + this.data.itemAngle, n = this.randomRange(this.data.minTurns, this.data.maxTurns), i = n;
                -1 !== this.data.maxDuration && (i = this.data.maxDuration);
                var r = this.randomRange(this.data.minDuration, i), o = 360 * n;
                -1 === this.data.angle ? o += e + this.data.itemAngle / 2 : o += this.randomRange(e, a), 
                this.setData({
                    lotteryRotateAngle: 0,
                    transitionDuration: 0,
                    spinning: !0
                }, function() {
                    setTimeout(function() {
                        t.setData({
                            lotteryRotateAngle: o,
                            transitionDuration: r
                        }), setTimeout(function() {
                            t.setData({
                                spinning: !1
                            }), t.triggerEvent("end", {
                                duration: r,
                                angle: o,
                                turns: n,
                                angleRange: {
                                    start: e,
                                    end: a
                                },
                                itemAngle: t.data.itemAngle
                            });
                        }, 1e3 * r);
                    }, 500);
                });
            }
        },
        randomRange: function(t, e) {
            return Math.floor(Math.random() * (e - t + 1) + t);
        },
        fillLatticeColor: function() {
            var t = [];
            this.data.prize.forEach(function(e) {
                e.color && t.push(e.color);
            }), t.length > 0 && (t = this.generateColors(t, this.data.prize.length)), this.setData({
                latticeColors: t
            });
        },
        generateColors: function() {
            for (var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [], e = arguments.length > 1 ? arguments[1] : void 0, a = t.length, n = t.length; n < e; n++) t.push(t[n - a]);
            return t;
        }
    },
    observers: {
        start: function(t) {
            !0 === t && this.startSpinning();
        }
    }
});